// pages/cinema/cinemaDetail/cinemaDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      cinemaData:'',
      movieId:'',
      curday:'',
      cinemaId:'',
      movie:'',
      selectIndex:0,
      date:'',
      movie_name:"",
      movie_category:"",
      movie_time:"",
      name:"",
      address:"",
      days:[
        {
          
          "m":"2月24日"
      }
        
      ],
      timeList:[
        {
          "tm":"12:00",
          "lang":"英语",
          "tp":"2D",
          "th":"一号厅"
        },
        {
          "tm":"15:00",
          "lang":"英语",
          "tp":"2D",
          "th":"二号厅"
        },
        {
          "tm":"18:00",
          "lang":"英语",
          "tp":"2D",
          "th":"三号厅"
        },
        {
          "tm":"21:00",
          "lang":"英语",
          "tp":"2D",
          "th":"三号厅"
        }
      ],
      divideDealList:'',
      success:0,
      scrollLeft:0,
      movies:[
        {
          "movie_id": 20001,
          "cat_id": 1,
          "movie_name": "满江红",
          "movie_price": "￥40起",
          "movie_score": 9.5,
          "movie_time": "159min",
          "movie_small_pic": "https://img2.doubanio.com/view/photo/s_ratio_poster/public/p2887555071.webp",
          "movie_category": "悬疑/喜剧/剧情",
          "movie_shangying_time": "2023-01-22 09:00",
        },
        {
          "movie_id": 20002,
          "cat_id": 2,
          "movie_name": "熊出没·伴我熊“芯”",
          "movie_price": "￥40起",
          "movie_score": 9.3,
          "movie_time": "96min",
          "movie_small_pic": "https://bkimg.cdn.bcebos.com/pic/9a504fc2d5628535e5ddf8a4dfb961c6a7efcf1bc5a2?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UyNzI=,g_7,xp_5,yp_5",
          "movie_category": "动画/喜剧/科幻",
          "movie_shangying_time": "2023-01-22 09:00",
        },
        {
          "movie_id": 20003,
          "cat_id": 3,
          "movie_name": "流浪地球2",
          "movie_price": "￥40起",
          "movie_score": 9.3,
          "movie_time": "173min",
          "movie_small_pic": "https://bkimg.cdn.bcebos.com/pic/9825bc315c6034a85edfa89095455e540923dd54d8de?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UyNzI=,g_7,xp_5,yp_5",
          "movie_category": "科幻/冒险/灾难",
          "movie_shangying_time": "2023-01-22 09:00",
        },
        {
          "movie_id": 20004,
          "cat_id": 4,
          "movie_name": "深海",
          "movie_price": "￥40起",
          "movie_score": 9.1,
          "movie_time": "112min",
          "movie_small_pic": "https://bkimg.cdn.bcebos.com/pic/08f790529822720e0cf323c9499d1d46f21fbe097df3?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5",
          "movie_category": "奇幻/动画",
          "movie_shangying_time": "2023-01-22 09:00",
        },
        {
          "movie_id": 20005,
          "cat_id": 5,
          "movie_name": "中国乒乓",
          "movie_price": "￥40起",
          "movie_score": 9.4,
          "movie_time": "137min",
          "movie_small_pic": "https://img9.doubanio.com/view/photo/m/public/p2887599076.webp",
          "movie_category": "剧情/运动",
          "movie_shangying_time": "2023-02-17 09:00",
        },
        {
          "movie_id": 20006,
          "cat_id": 6,
          "movie_name": "不能流泪的悲伤",
          "movie_price": "￥40起",
          "movie_score": 9.0,
          "movie_time": "97min",
          "movie_small_pic": "https://bkimg.cdn.bcebos.com/pic/c83d70cf3bc79f3df8dcfec0c3f7da11728b461083bf?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxMTY=,g_7,xp_5,yp_5",
          "movie_category": "爱情",
          "movie_shangying_time": "2023-02-14 09:00",
        },
        {
          "movie_id": 20007,
          "cat_id": 7,
          "movie_name": "无名",
          "movie_price": "￥40起",
          "movie_score": 9.1,
          "movie_time": "128min",
          "movie_small_pic": "https://bkimg.cdn.bcebos.com/pic/79f0f736afc379310a5537a4b792a04543a98326bbba?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UyNzI=,g_7,xp_5,yp_5",
          "movie_category": "悬疑/动作/剧情",
          "movie_shangying_time": "2023-01-22 09:00",
        },
        {
          "movie_id": 20008,
          "cat_id": 8,
          "movie_name": "阿凡达·水之道",
          "movie_price": "￥40起",
          "movie_score": 9.1,
          "movie_time": "192min",
          "movie_small_pic": "https://img9.doubanio.com/view/photo/m/public/p2884182275.webp",
          "movie_category": "科幻/动作/冒险",
          "movie_shangying_time": "2022-12-16",
        },
        {
          "movie_id": 20009,
          "cat_id": 9,
          "movie_name": "黑豹2",
          "movie_price": "￥40起",
          "movie_score": 8.4,
          "movie_time": "161min",
          "movie_small_pic": "https://bkimg.cdn.bcebos.com/pic/eac4b74543a98226cffc8f34dfd4ae014a90f603bdd0?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxODA=,g_7,xp_5,yp_5",
          "movie_category": "动作/冒险",
          "movie_shangying_time": "2023-02-07",
        },
        {
          "movie_id": 20010,
          "cat_id": 10,
          "movie_name": "交换人生",
          "movie_price": "￥40起",
          "movie_score": 8.7,
          "movie_seen": 7797030,
          "movie_time": "110min",
          "movie_small_pic": "https://img1.doubanio.com/view/photo/m/public/p2886396440.webp",
          "movie_category": "喜剧/家庭",
          "movie_shangying_time": "2023-01-22 09:00",
        }
      ],
      select:0
  },

  bindDateChange(e){
      this.setData({
          selectIndex:e.currentTarget.dataset.index,
          date:e.currentTarget.dataset.date,
          timeList:this.data.movie.shows[e.currentTarget.dataset.index].plist

      })

      console.log(this.data.timeList);

      console.log(this.data.date)
  },
  tobuyTicket(e){
      var that=this;
     console.log(e.currentTarget.dataset.info);
     var tm=e.currentTarget.dataset.info.tm;
     wx.navigateTo({
       url: '../../order/order?username=zrh&cinemaid='+that.data.cinemaId+'&time='+tm+'&moviceid='+that.data.movieId+'&price='+e.currentTarget.dataset.info.vipPrice+'&moviceName='+that.data.movie.nm+'&cinemaName='+that.data.cinemaData.nm,
      // url:'../../order/order?obj='+e.currentTarget.dataset.info
     })
     
  },
  selectMovie(e){
      var that=this;
      console.log(e.currentTarget.dataset.index);
      console.log(e.currentTarget.dataset.movie);
      var movieId=e.currentTarget.dataset.movie.id;
      that.setData({
          select:e.currentTarget.dataset.index,
          movie:e.currentTarget.dataset.movie,
          movieId:e.currentTarget.dataset.movie.id
      })
      wx.request({
        url: 'https://m.maoyan.com/ajax/cinemadetail?cinemaId='+that.data.cinemaId,
        success(res){
            console.log(res.data);


            for(var i in res.data.showData.movies){                               //根据moviceID找档期
              if(res.data.showData.movies[i].id==movieId){
                  res.data.showData.movies[i].img=res.data.showData.movies[i].img.replace(/w.h/g, "372.550");  
                that.setData({
                    movie:res.data.showData.movies[i],
                    timeList:res.data.showData.movies[i].shows[0].plist,         //默认
                    success:1
                })
               
                for(var k in that.data.movie.shows){                    //根据moviceid找日期
                  that.data.days=[];
                    that.data.days.push(that.data.movie.shows[k].dateShow);
                    that.setData({
                        days:that.data.days
                    })
                }
              }
              console.log(that.data.success);
          }
          if(!that.data.movie){
            that.setData({
                success:0
            })
        }

        console.log(that.data.movie);


        that.setData({
            scrollLeft:that.data.scrollLeft+100
        })

        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this;
      const { cinemaId = '', movieId = '', day = ''} = options
      console.log(cinemaId);
      console.log(movieId);
      console.log(day);

      this.setData({
          cinemaId,
          movieId,
          curday:day
      })

      // this.onLoad();
      wx.request({
        url: 'https://m.maoyan.com/ajax/cinemadetail?cinemaId='+cinemaId,
        success(res){
            console.log(res.data)
            console.log(res.data.cinemaData);
            for(var i in res.data.showData.movies){
              res.data.showData.movies[i].img=res.data.showData.movies[i].img.replace(/w.h/g, "372.550");
          
          that.setData({
              movies:res.data.showData.movies
          })
      }


          if(res.data.dealList.divideDealList.length!=0){         //判断是否有小吃服务
              console.log(1);
              for(var i in res.data.dealList.divideDealList){
                  for(var k in res.data.dealList.divideDealList[i].dealList)
                  {
                  res.data.dealList.divideDealList[i].dealList[k].imageUrl=res.data.dealList.divideDealList[i].dealList[k].imageUrl.replace(/w.h/g, "440.0");
                  console.log(123)
                  }
              }
          }
          
          


              if(movieId){
            for(var i in res.data.showData.movies){                               //根据moviceID找档期
                if(res.data.showData.movies[i].id==movieId){
                    
                  that.setData({
                      movie:res.data.showData.movies[i],
                      timeList:res.data.showData.movies[i].shows[0].plist,         //默认显示第一天的档期
                      divideDealList:res.data.dealList.divideDealList,
                      success:1,
                      select:i
                  })
                  console.log(that.data.select)
                 
                  for(var k in that.data.movie.shows){                    //根据moviceid找日期
                      that.data.days.push(that.data.movie.shows[k].dateShow);
                      that.setData({
                          days:that.data.days
                      })
                  }
                }
                console.log(that.data.success);
            }
            if(!that.data.movie){
              that.setData({
                  success:0
              })
          }
          


          } else{
              
              that.setData({
                  movie:res.data.showData.movies[0],
                  timeList:res.data.showData.movies[0].shows[0].plist,
                  divideDealList:res.data.dealList.divideDealList,
                  success:1
              })
              for(var k in that.data.movie.shows){                    //日期
                  that.data.days.push(that.data.movie.shows[k].dateShow);
                  that.setData({
                      days:that.data.days
                  })
              }


          }
            that.setData({
                cinemaData:res.data.cinemaData
            })
            console.log(that.data.movie);
            that.setData({
                select:that.data.select
            })
           
          //   for(var i in res.data.showData.movies){
          //     if(res.data.showData.movies[i].id==movieId){
          //       that.setData({
          //           movie:res.data.showData.movies[i]
          //       })
          //     }
          // }


        }
      })
      

      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      console.log(this.data.selectIndex)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages  = getCurrentPages();
      let currentPage = pages[pages.length-1] ;
      let options = currentPage.options ;
      this.setData({
        movie_name:options.movie_name,
        movie_time:options.movie_time,
        movie_category:options.movie_category,
        name:options.name,
        address:options.address
      })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})